using System;
using System.Drawing;
using System.Collections.Generic;
using TTDotNet.Engine.Stations;
using TTDotNet.Engine.GUI.Windows;
using TTDotNet.Engine.GUI.Buttons;
using Microsoft.DirectX.Direct3D;
using TTDotNet.Engine.Industries;

namespace TTDotNet.Engine.GUI.Windows.Info
{
    public class StationInfoWindow : Window
    {
        private int cSize = 24;
        private int bWidth = 100;
        private int border = 2;
        private int waitingHeight = 100;
        private int waitingItems = 4;
        private int acceptsHeight = 50;

        private ListBox waitingLB;
        private Station station;

        private Dictionary<CargoType, ListableItem> listItems;

        public StationInfoWindow(PointF screenLocation, Station station)
        {
            listItems = new Dictionary<CargoType, ListableItem>();
            this.station = station;

            int width = (bWidth * 4) + (5 * border);
            int height = (2 * cSize) + waitingHeight + acceptsHeight + (5 * border);

            SetScreenRectangle(new RectangleF(screenLocation, new SizeF(width, height)));

            int controlNum = 0;

            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            //Close button. 
            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - (cSize + (3 * border)), cSize)),
                                            station.Name, 14, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            waitingLB = new ListBox(new RectangleF(new PointF(border, cSize + (2 * border)), new SizeF(width - (2 * border), waitingHeight)),
                                            18, "", 1, Color.Black, "Arial", (float)waitingHeight / (float)waitingItems, Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"));

            windowControls[controlNum++] = waitingLB;


            float itemHeight = waitingLB.ItemHeight;

            ListableItem listItem = waitingLB.AddItem();
            Label label = new Label(new RectangleF(new PointF(border, 0), new SizeF(listItem.Area.Width - border, listItem.Area.Height)), "Waiting:", 14,
                                            Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), DrawTextFormat.Left | DrawTextFormat.VerticalCenter);

            listItem.AddControl(label);
            waitingLB.AlwaysDraw = 1;

            foreach (CargoType type in station.CargoWaiting.Keys)
            {
                AddNewItem(type, station.GetAmountWaiting(type));
            }

            string accepts = "Accepts: ";

            for (int i = 0; i < station.Accepts.Count; i++)
            {
                accepts += station.Accepts[i].ToString();

                if (i != station.Accepts.Count - 1) accepts += ", ";
            }

            windowControls[controlNum++] = new Label(new RectangleF(new PointF(border, cSize + (3 * border) + waitingHeight), new SizeF(width - (2*border), acceptsHeight)), accepts, 14, Color.White, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), DrawTextFormat.Left);

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF(border, height - cSize - border), new SizeF(bWidth, cSize)), "Location", 14, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));
            windowControls[controlNum - 1].OnControl += new Control.ControlDelegate(StationInfoWindow_OnLocation);

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((2 * border) + bWidth, height - cSize - border), new SizeF(bWidth, cSize)), "Ratings", 14, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));

            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((3 * border) + (2 * bWidth), height - cSize - border), new SizeF(bWidth, cSize)), "Rename", 14, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), Game.textureCacher.LoadTexture("artwork/colours/silver.jpg"));
            windowControls[controlNum - 1].OnControl += new Control.ControlDelegate(StationInfoWindow_OnRename);

        }

        void StationInfoWindow_OnLocation(object data, object tag)
        {
            //Game.myCamera.MoveForward(0.1f);
        }

        void StationInfoWindow_OnRename(object data, object tag)
        {
            //station.CargoWaiting[CargoType.Wood] -= 0.5f;
        }

        private ListableItem AddNewItem(CargoType type, float amount)
        {
            ListableItem listItem = waitingLB.AddItem();

            Label label = new Label(new RectangleF(new PointF(border, 0), new SizeF(listItem.Area.Width - (border * 2), listItem.Area.Height)), Math.Floor(amount) + " " + Cargo.GetQuantityAndUnit(type), 14,
                                            Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdGrey.jpg"), DrawTextFormat.Right | DrawTextFormat.VerticalCenter);
            listItem.AddControl(label);

            return listItem;
        }

        public override void Update()
        {
            waitingLB.RemoveFromOnwards(1);

            foreach (CargoType type in station.CargoWaiting.Keys)
            {
                if (Math.Floor(station.GetAmountWaiting(type)) > 0)
                {
                    if (listItems.ContainsKey(type))
                    {
                        waitingLB.Add(listItems[type]);
                    }
                    else
                    {
                        listItems[type] = AddNewItem(type, station.GetAmountWaiting(type));
                    }

                    listItems[type].GetControl(0).Text = Math.Floor(station.GetAmountWaiting(type)) + " " + Cargo.GetQuantityAndUnit(type);
                }
            }
        }

    }
}
